package redis.task;

import com.fasterxml.jackson.annotation.JsonTypeInfo;

import java.util.concurrent.atomic.AtomicLong;

public class DeferredResultTask<T> {
    private static final AtomicLong id = new AtomicLong(0);
    private long taskId;        // 任务消息流水号
    private String serverId;    // 执行任务主机ID，可以从NettySession中获取
    private String fromServerId;
    private String sessionId;   // NettySession的sessionId
    private String type;        // 任务类型
    private long timeout = 60000;    // 任务超时时间，单位秒。默认60秒
    @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
    private T data;

    public DeferredResultTask() { }

    public DeferredResultTask(String serverId, String fromServerId, String sessionId, String type, T data) {
        this.taskId = nextTaskId();
        this.serverId = serverId;
        this.fromServerId = fromServerId;
        this.sessionId = sessionId;
        this.type = type;
        this.data = data;
    }

    public long nextTaskId() {
        return id.incrementAndGet();
    }

    public long getTaskId() {
        return taskId;
    }

    public void setTaskId(long taskId) {
        this.taskId = taskId;
    }

    public String getServerId() {
        return serverId;
    }

    public void setServerId(String serverId) {
        this.serverId = serverId;
    }

    public String getFromServerId() {
        return fromServerId;
    }

    public void setFromServerId(String fromServerId) {
        this.fromServerId = fromServerId;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public long getTimeout() {
        return timeout;
    }

    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
